<?php

// Created by Khanh Nam
class DispatchController extends Controller{
    public $layout='//layouts/column2';
    
    public function filters(){
        return array(
            'accessControl'
        );
    }
    public function accessRules(){
        return array(
            array(
                'allow',
                'users'=>array('@'),
            ),
            array(
                'deny',
                'roles'=>array('*')
            )
        );
    }
    public function actionIndex(){
        $this->render('index');
    }
    
    public function actionNewdispatch(){
        $model = new Dispatchdetails;
        $model->custno = '0000571';
        $model->locno = '00001';
        $model->department = 'CORP - Internal Dispatches';
        $model->calledinby = Yii::app()->user->model->real_name;
        if(isset($_POST['Dispatchdetails'])){
            unset($_POST['Dispatchdetails']['custno']);
            unset($_POST['Dispatchdetails']['locno']);
            unset($_POST['Dispatchdetails']['department']);
            $model->attributes = $_POST['Dispatchdetails'];

            if($model->save()){
                $this->redirect(array('/dispatchdetails/update','dispatch'=>$model->dispatch));
            }
        }
        $this->render('newdispatch',array(
            'model'=>$model
        ));
    }

    public function actionBilledDispatchList(){
        $this->render('billedDispatchList');
    }
    public function actionGetBilledDispatchList($level = 1, $page = 0,$sort = null){
        $order = CJSON::decode($sort);
        $crit = new CDbCriteria();
        if($level == 1){
            $crit->compare('billingtype',0);
        }else if($level == 2){
            $crit->addCondition('billingtype != 0 and billingtype !=2');
        }
        $crit->addCondition('complete is not null  and invoiced =0');
        $crit->addCondition('complete is not null and invoiced = 1 and invoice is not null','or');
        $crit->order = 'dispatch';
        $data = new CActiveDataProvider('Dispatchdetails',array(
            'criteria'=>$crit,
            'pagination'=>array(
                'pageSize'=>20,
                'currentPage'=>$page
            )
        ));
        $rs = array();
        $rs['totalCount'] = $data->getTotalItemCount();
        $items = $data->getData();
        foreach($items as $i=>$item){
            $rs['data'][$i] = $item->attributes;
            if($item->customer!=NULL){
                $rs['data'][$i]['custno']=$item->customer->lastname;
                $rs['data'][$i]['custCity']=$item->customer->city;
            $rs['data'][$i]['custAdd']=$item->customer->add1;
            $rs['data'][$i]['custState']=$item->customer->state;
            }
            $rs['data'][$i]['billingtypeText'] = Dropdowns::getText($item->billingtype);
            if($item->location != null){
                $rs['data'][$i]['locName'] = $item->location->locname;
            }
            
            if($item->lastestTech!=NULL){
                $rs['data'][$i]['techStatus']=$item->lastestTech!=NULL?$item->lastestTech->status:NULL;
                $rs['data'][$i]['technician'] = $item->lastestTech->employee->firstname . ' ' . $item->lastestTech->employee->lastname;
            }
        }

        echo json_encode($rs);
        Yii::app()->end();
    }

    public function actionList($incomplete = true, $billtype = null,$page=0,$sort=NULL){
        if($page>0) $page--;
        $order=CJSON::decode($sort);
        $crit= new CDbCriteria();
        if($incomplete)
        {
            $crit->addCondition('complete = "" or complete is null');
        }
        $crit->compare('billtype',$billtype);
        if($sort)
        {
            $crit->order = $order[0]['property'].' '.$order[0]['direction'];
        }
        $data = new CActiveDataProvider('Dispatchdetails',array(
            'criteria'=>$crit,
            'pagination'=>array(
                'pageSize'=>20,
                'currentPage'=>$page
            )
        ));

        if($data!=NULL && count($data->getData())>0){
            $rs=array();
            $rs['totalCount']=$data->getTotalItemCount();
            foreach($data->getData() as $i=>$item){

                $rs['data'][$i]=$item->getAttributes();
                if($item->customer!=NULL){
                    $rs['data'][$i]['custno']=$item->customer->lastname;
                    $rs['data'][$i]['custCity']=$item->customer->city;
                    $rs['data'][$i]['custAdd']=$item->customer->add1;
                    $rs['data'][$i]['custState']=$item->customer->state;
                }
                
                if($item->lastestTech!=NULL){
                    $rs['data'][$i]['techStatus']=$item->lastestTech!=NULL?$item->lastestTech->status:NULL;
                    $rs['data'][$i]['technician'] = $item->lastestTech->employee->firstname . ' ' . $item->lastestTech->employee->lastname;
                }                                
                $rs['data'][$i]['billingtypeText'] = Dropdowns::getText($item->billingtype);
            }   
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
}
?>
